//
//  AccountViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-12.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "AccountViewController.h"
#import "PasswordChangeViewController.h"
#import "SignUpViewController.h"
#import "UIViewController+BarItem.h"
#import "CustomTabBar.h"
#import "CustomTabBarViewController.h"
#import "RegisterViewController.h"
#import "CodeViewController.h"


#define kExampleMovie   @"功夫熊猫2"

@interface AccountViewController ()

- (void)configureCell:(AccountTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (void)changeAccount;
- (void)showSignUpView;
- (void)showRegisterView;
- (void)makePhoneCallToKKZ;

@end

@implementation AccountViewController

@synthesize unPaidCoupon;
@synthesize unUsedCoupons;
@synthesize tmpCell;
@synthesize cellNib;
@synthesize accountView;

- (id)init
{
    self = [super init];
    if (self) {
        
    }
    
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [unPaidCoupon release];
    [unUsedCoupons release];
    
    userName_ = nil;
    passWord_ = nil;
    myCity_ = nil;
    myDistrict_ = nil;
    myRegion_ = nil;
    
    signUpButton_ = nil;
    registerButton_ = nil;
    telButton_ = nil;
    
    [super dealloc];
}

#pragma mark - View lifecycle


- (void)reloadView
{
    if (USER_DEFAULT_NAME) {
        
        // Load the user info
        userName_ = USER_DEFAULT_NAME;
        
        // Normal account view
        [self createBarItemOn:kNavBarItemSideRight frame:kNavBarRigthItemFrameWithPrompt title:@"帐号选项" target:self action:@selector(changeAccount)];
        
        // Set the table view style
        self.accountView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
        
        // Reload the table view data
        [self.accountView reloadData];
        
        // Set the subviews
        self.accountView.hidden = NO;
        [self.view bringSubviewToFront:self.accountView];
    }
    else {
        // No account stored in app, so show only sign up and register button
        self.accountView.hidden = YES;
        
        // Set the sign up button
        signUpButton_ = [UIButton buttonWithType:UIButtonTypeCustom];
        [signUpButton_ setFrame:CGRectMake(20, 20, 280, 37)];
        [signUpButton_ setTitle:@"登录" forState:UIControlStateNormal];
        [signUpButton_ setBackgroundImage:[UIImage imageNamed:@"detail_coupon_cell.png"] forState:UIControlStateNormal];
        [signUpButton_ addTarget:self action:@selector(showSignUpView) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:signUpButton_];
        
        // Set the register button
        registerButton_ = [UIButton buttonWithType:UIButtonTypeCustom];
        [registerButton_ setFrame:CGRectMake(20, 70, 280, 37)];
        [registerButton_ setTitle:@"注册" forState:UIControlStateNormal];
        [registerButton_ setBackgroundImage:[UIImage imageNamed:@"detail_coupon_cell.png"] 
                                   forState:UIControlStateNormal];
        [registerButton_ addTarget:self action:@selector(showRegisterView) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:registerButton_];
        
        // Set the tel button
        telButton_ = [UIButton buttonWithType:UIButtonTypeCustom];
        [telButton_ setFrame:CGRectMake(20, 120, 280, 37)];
        [telButton_ setTitle:@"联系客服" forState:UIControlStateNormal];
        [telButton_ setBackgroundImage:[UIImage imageNamed:@"detail_coupon_cell.png"] forState:UIControlStateNormal];
        [telButton_ addTarget:self action:@selector(makePhoneCallToKKZ) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:telButton_];
        
    }

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Set the cell nib file
    self.cellNib = [UINib nibWithNibName:@"AccountTableViewCell" bundle:nil];
    
    // Set the coupons data
    self.unUsedCoupons = [NSArray arrayWithObjects:@"功夫熊猫2", @"变形金刚3", @"源代码", nil];
    
    // Set the nav bar title
    [self addTitleToNavigationBar:@"个人帐户"];
    
    //
    [self reloadView];
    
    // Set view background
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - 
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section = 1;
    if ([self.unUsedCoupons count] > 0) {
        section += 1;
    }
    if ([self.unPaidCoupon count] > 0) {
        section += 1;
    }
    return section;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        if (self.unUsedCoupons) {
            return 1;
        }
    }
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    
    AccountTableViewCell *cell = (AccountTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        [self.cellNib instantiateWithOwner:self options:nil];
        cell = self.tmpCell;
        self.tmpCell = nil;
    }
    
    [self configureCell:cell atIndexPath:indexPath];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"帐号信息";
    }
    else if ([self.unUsedCoupons count] > 0 && section == 1) {
        return @"未使用项目";
    }
    else if ([self.unUsedCoupons count] == 0 && section == 1 && [self.unPaidCoupon count] > 0) {
        return @"未支付项目";
    }
    else if ([self.unPaidCoupon count] > 0 && section == 2) {
        return @"未支付项目";
    }
    else {
        return @"";
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0f;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 1) {
        CodeViewController *viewController = [[CodeViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }
    
    [self.accountView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark - ActionSheet Delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 7) {         // 修改密码
        PasswordChangeViewController *viewController = [[PasswordChangeViewController alloc] init];
        UINavigationController *navController = [[UINavigationController alloc] 
                                                 initWithRootViewController:viewController];
        [self presentModalViewController:navController animated:YES];
        [navController release];
        [viewController release];
    }
    else if (buttonIndex == 0) {    // 重新登录
        SignUpViewController *viewController = [[SignUpViewController alloc] initWithFrame:self.view.frame signUpEscape:YES];
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }
}

#pragma mark -
#pragma mark - Sign Up Delegate

- (void)newAccountDidSignUp
{
    [self reloadView];
}

#pragma mark -
#pragma mark - Private Methods


- (void)configureCell:(AccountTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    // Set the intro label
    NSUInteger section = [indexPath section];

    if (section == 0) {
        [cell setCellIntro:@"手机号" Info:userName_];    
    }
    else if (section == 1) {
        [cell setCellIntro:@"影片名称" Info:[unUsedCoupons objectAtIndex:indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if (section == 2) {
        if (indexPath.row == 0) {
            [cell setCellIntro:@"影片名称" Info:kExampleMovie];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else {
            [cell setCellIntro:@"影片价格" Info:@"120元"];
        }
    }
    
    // Set the background color
    [cell setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"account_cell.png"]]];
}

- (void)changeAccount
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                   delegate:self 
                                          cancelButtonTitle:@"取消" 
                                     destructiveButtonTitle:nil 
                                          otherButtonTitles:@"重新登录", nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)showSignUpView
{
    NSLog(@"sign");
    SignUpViewController *viewController = [[SignUpViewController alloc] initWithFrame:[UIScreen mainScreen].applicationFrame signUpEscape:NO];
    viewController.delegate = self;
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)showRegisterView
{
    NSLog(@"reg");
    RegisterViewController *viewController = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)makePhoneCallToKKZ
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://13717719950"]];
}

@end
